package com.foxmail.yusp.mycloud;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettingActivity extends AppCompatActivity {

    private final static String TOPIC = "esp8266_E142CD/rpc";
    @BindView(R.id.title)
    EditText et_title;
    @BindView(R.id.sensors)
    Spinner sensors;
    @BindView(R.id.ssid)
    EditText et_ssid;
    @BindView(R.id.pass)
    EditText et_pass;
    @BindView(R.id.server)
    EditText et_server;
    @BindView(R.id.user)
    EditText et_user;
    @BindView(R.id.pass_mqtt)
    EditText et_pass_mqtt;
    @BindView(R.id.btnRefresh)
    Button btnFrefresh;
    private MyMQTT myMQTT;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        // bind
        ButterKnife.bind(this);
        // 焦点
        btnFrefresh.setFocusable(true);

        myMQTT = new MyMQTT(getApplicationContext(), "/th/#",
                "tcp://m13.cloudmqtt.com:18957", "yusp", "60240abc");
        myMQTT.setStateListener(new MyMQTT.StateListener() {
            @Override
            public void stateListner(String state) {
                if (state.equals("连接成功")) {
                    myMQTT.pub(TOPIC, "{\"args\": {\"type\": \"sta\"}, " +
                            "\"tag\": \"wifi\", \"method\": \"my.get\",\"src\": \"/th\"}");
                }
            }
        });
        myMQTT.setMsgListener(new MyMQTT.MsgListener() {
            @Override
            public void msgListner(JSONObject obj) {
                try {
                    JSONObject obj_2 = new JSONObject(obj.getString("result"));
                    String tag = obj.getString("tag");
                    if (tag.equals("wifi")) {
                        et_ssid.setText(obj_2.getString("ssid"));
                        et_pass.setText(obj_2.getString("pass"));
                    } else if (tag.equals("mqtt")) {
                        et_server.setText(obj_2.getString("server"));
                        et_user.setText(obj_2.getString("user"));
                        et_pass_mqtt.setText(obj_2.getString("pass"));
                    } else if (tag.equals("dev")) {
                        et_title.setText(obj_2.getString("title"));
                        //sensors.setPrompt(obj_2.getString("th"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        myMQTT.connect();

    }

    //按钮：刷新
    @OnClick(R.id.btnRefresh)
    public void btn_refresh(View view) {
        myMQTT.pub("esp8266_E142CD/rpc", "{\"args\": {\"type\": \"sta\"}, " +
                "\"tag\": \"wifi\", \"method\": \"my.get\",\"src\": \"/th\"}");
        myMQTT.get_key("mqtt");
        myMQTT.get_key("dev");
        //get_key("r0");
    }

    //按钮：保存
    @OnClick(R.id.btnSave)
    public void btn_save(View view) {

        Gson gson = new Gson();
        //mqtt
        String json;
        json = gson.toJson(new MqttFrame(new MqttFrame.Config(new MqttFrame.MQTT(et_server.getText().toString(),
                et_user.getText().toString(), et_pass_mqtt.getText().toString()))));
        myMQTT.put_key(json, "set_mqtt");
        //wifi
        json = gson.toJson(new WifiFrame(new WifiFrame.Config(new WifiFrame.Wifi(new WifiFrame.Sta(et_ssid.getText().toString(),
                et_pass.getText().toString())))));
        myMQTT.put_key(json, "set_wifi");
        //dev
        json = gson.toJson(new DevFrame(new DevFrame.Config(new DevFrame.Dev(et_title.getText().toString(),
                sensors.getSelectedItem().toString()))));
        myMQTT.put_key(json, "set_dev");
    }

//
}
